package cn.edu.nuc.ssm.controller.film;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.nuc.ssm.controller.page.Pager;
import cn.edu.nuc.ssm.domain.Cinema;
import cn.edu.nuc.ssm.service.interfaces.CinemalService;

@Controller
@RequestMapping("cinema")
public class CinemalController {
    @Autowired
	private CinemalService service;
    
    /**
	 * 通过servlet进入添加影城页面。
	 * @return
	 */
    @RequestMapping(value="add",method=RequestMethod.GET)
    public String getAdd(){
    	return "film/cinema/add";
    }
    /**
     * 添加影城（post）
     * @param Cinema
     * @return
     */
    @RequestMapping(value="add",method=RequestMethod.POST)
    public String addCinema(Cinema cinema){
    	int result = service.insert(cinema);
    	if(result == 1){
    		return "redirect:list";
    	}
    	return "error";
    } 
	/**
	 * 进入影城列表(查询)
	 * @return
	 */
	@RequestMapping(value="list",method=RequestMethod.GET)
	public String getFun(@RequestParam(name="cinemaname",defaultValue="") String cinemaname,
			@RequestParam(name="current",defaultValue="1") int current , 
			Model model){
		Pager<Cinema> pager = service.pageList(cinemaname, current);
		model.addAttribute("pager", pager);
		return "film/cinema/list";
	}
	
	/**
	 * 更新，（全部进行更新）
	 */
	//获取功能信息
	@RequestMapping(value="detail/{cinemaid}",method=RequestMethod.GET)
	public String updateCinema(@PathVariable("cinemaid") Integer cinemaid,Model model){
		Cinema cinema = service.selectByFcid(cinemaid);
		model.addAttribute("cinema", cinema);
		return "film/cinema/update";
	}
	//更新
	@RequestMapping(value="update/{cinemaid}",method=RequestMethod.PUT)
	public String updateCinema(@PathVariable("cinemaid") Integer cinemaid,Cinema cinema){
		int result = service.updateByPrimaryKey(cinema);
		if(result == 1){
			return "redirect:/cinema/list";	
		}
		return  "redirect:error";
	}
	//删除单个影城信息。
	@RequestMapping(value="delete/{cinemaid}",method=RequestMethod.DELETE)
	public String deleteFun(@PathVariable("cinemaid") Integer cinemaid){
		int result = service.deleteByPrimaryKey(cinemaid);
		if(result==1){
			return "redirect:/cinema/list";	
		}
		return  "error";
	}
    
	
	//全部影城名称，添加影厅时首先选择影城。。。。Ajax
	@ResponseBody
	@RequestMapping(value="select",method=RequestMethod.GET)
	public List<Cinema> select(){
		List<Cinema> list = service.select();
		if(list.size()>0){
			return list;
		}
		return null;
	}
    
}
